<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>社科基金申报</span>
      </div>
    </template>

    <div class="search-form">
      <!-- 搜索区域 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-row>
          <el-form-item>
            <el-input v-model="formInline.name" placeholder="请输入项目名称" clearable @keyup.enter="onSubmit"></el-input>
          </el-form-item>

          <el-form-item>
            <el-select v-model="formInline.achievementForm" placeholder="请选择成果形式" clearable>
              <el-option label="专著或论文" value="1" />
              <el-option label="研究报告" value="2" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('search')">
              <el-icon>
                <Search />
              </el-icon>
            </el-button>
          </el-form-item>
        </el-row>
      </el-form>

      <div>
        <el-button type="primary" @click="addFund">新建</el-button>
      </div>
    </div>

    <div class="table-data" v-loading="loading">
      <el-table
        :data="fundData"
        stripe
        borderresizable
        highlight-hover-row
        show-overflow
        keep-source
        scrollbar-always-on
      >
        <el-table-column
          v-for="(tableItem, tableIndex) in fundTableColumns" :key="tableIndex"
          :prop="tableItem.field"
          :label="tableItem.label"
          :min-width="tableItem.minWidth"
          :formatter="tableItem.formatter"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          fixed="right"
          field="opera"
          label="操作"
          min-width="120"
        >
          <template #default="{ row }">
            <el-button
              type="warning"
              link
              @click="fundDetail(row)"
            >查看</el-button>
            <el-button
              type="success"
              link
              @click="fundEdit(row)"
              v-if="row.checkStatus == 0"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="table-page">
        <el-pagination background layout="prev, pager, next" :total="dataTotal" :page-size="pageSize" :current-page="currentPage" @current-change="currentChange" />
      </div>
    </div>
  </el-card>
</template>

<script>
export default { name: 'backendLearnfund' }
</script>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Search, Filter } from '@element-plus/icons-vue'
import { http } from '@/utils/http/index';
import { dateFormatter, checkStatusFormatter } from "@/utils/backend.js"
import { useAuthStore } from '@/stores/modules/auth.js';
import { ContentTypeEnum } from '@/enums/httpEnum';

/* 路由 */
const route = useRouter()

/* 用户信息 */
const authStore = useAuthStore()

onMounted(() => {
  onSubmit()
})

/* 左侧搜索表单 */
const formInline = ref({
  name: "",
  achievementForm: "",
})

/* 分页 */
const dataTotal = ref(0)
const pageSize = ref(20)
const currentPage = ref(1)

if(route.currentRoute.value.query.currentPage) {
  currentPage.value = Number(route.currentRoute.value.query.currentPage)
}

/* 切换页码方法 */
const currentChange = (val) => {
  currentPage.value = val
  onSubmit()
}

/* 表格数据加载动画 */
const loading = ref(false)

/* 表格数据 */
const fundData = ref([])

const achievementFormFormatter = (row, column, cellValue) => {
  return cellValue ? cellValue == '1' ? '专著或论文' : '研究报告' : ''
}

const checkstatusFormatter = (row, column, cellValue) => {
  return cellValue == '1' || cellValue == '0'  ? '待审批' : cellValue == '2' ? '审批通过' : cellValue == '3' ? '审批未通过' : ''
}

/* 表格字段 */
const fundTableColumns = reactive([
  { field: "name", label: "项目名称", minWidth: "200" },
  { field: "achievementForm", label: "成果形式", minWidth: "200", formatter: achievementFormFormatter },
  { field: "projectLeader", label: "项目负责人", minWidth: "120" },
  { field: "unit", label: "负责人所在单位", minWidth: "150" },
  { field: "fillTime", label: "填表日期", minWidth: "120", formatter: dateFormatter },
  { field: "persons", label: "主要参加者", minWidth: "140" },
  { field: "checkStatus", label: "状态", minWidth: "120", formatter: checkstatusFormatter },
])

/* 点击搜索 */
const onSubmit = (type) => {
  loading.value = true
  let params = {
    pageIndex: currentPage.value,
    pageSize: pageSize.value,
    orderbyCol: "fillTime",
  }
  let dfi = {
    Logic: 0,
    Filters: [{
      Field: "createuserid",
      Operator: 8,
      Value: authStore.userInfo.uid
    }]
  }
  for (let k in formInline.value){
    if(formInline.value[k]){
      dfi.Filters.push({
        field: k,
        operator: 0,
        value: formInline.value[k]
      })
    }
  }
  
  if(dfi.Filters.length){
    params.dfi = dfi
  }

  http.$POST("/DeclarationM/GetSocialScienceFundInfoList", params).then(res => {
    loading.value = false
    if (res.isError) return
    fundData.value = res.result.dataLst
    dataTotal.value = res.result.countAll
  }).catch(err => {
    loading.value = false
    ElMessage.warning("查询失败，请重试")
  })
}

/* 点击新建 */
const addFund = () => {
  route.push({
    path: "/backend/learn-fund/add",
    query: { type: 'add' }
  })
}

/* 点击编辑 */
const fundEdit = (row) => {
  route.push({
    path: "/backend/learn-fund/add",
    query: { id: row.id, type: 'edit', currentPage: currentPage.value }
  })
}

/* 查看详情 */
const fundDetail = (row) => {
  let { href } = route.resolve({
    path: "/backend/learn-fund/detail",
    query: { id: row.id, flowId: row.flowId, currentPage: currentPage.value }
  })
  window.open(href, "_blank")
}
</script>

<style lang='scss' scoped>
@import "@/assets/styles/backendElement.scss";
.filter-button{
  :deep(.el-form-item__content){
    justify-content: center;
  }
}
</style>